package com.litq.education.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.litq.education.bean.GroupTable;
import com.litq.education.mapper.CustomerMapper;
import com.litq.education.mapper.GroupMapper;
import com.litq.education.service.GroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * GroupServiceImpl 实现类
 *
 * @author litq
 * @version v1.0
 * @since 2019/2/10 1:31
 */

@Service("GroupService")
public class GroupServiceImpl implements GroupService {
    private static final Logger logger = LoggerFactory.getLogger(GroupServiceImpl.class);
    @Resource

    private GroupMapper groupMapper;
    private CustomerMapper CustomerMapper;

    /**
     * 查询活动表id和名称，用于前台下拉框
     *
     * @return 返回list
     * @Date 2019-8-14
     * @author Litq
     * @version 1.0
     */
    @Override
    public List queryAllGroup() {
        QueryWrapper<GroupTable> queryWrapper = new QueryWrapper();
        queryWrapper.select("id", "group_name");
        List list = groupMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public int selectListCount() {

        logger.info("----------------------------------开始进行查询逻辑处理");
        int sign = groupMapper.count();
        return sign;
    }

    /**
     * 查询活动表id和名称，用于前台下拉框
     *
     * @return 返回list
     * @Date 2019-8-14
     * @author Litq
     * @version 1.0
     */
    @Override
    public Page<Map<String, Object>> selectListPage(int current, int number) {
        logger.info("----------------------------------开始进行查询逻辑处理");
        // 新建分页
        Page<Map<String, Object>> page = new Page<Map<String, Object>>(current, number);

        // 返回分页结果 1为id
        logger.info("----------------------------------查询结果打印" + page.setRecords(this.groupMapper.dyGetGroupList(page, 1)));
        return page.setRecords(this.groupMapper.dyGetGroupList(page, 1));

    }

    /**
     * 查询活动名
     *
     * @return 返回list
     * @Date 2019-8-14
     * @author Litq
     * @version 1.0
     */
    @Override
    public Page<Map<String, Object>> selectListPageByUserName(int current, int number, String group_name) {
        logger.info("----------------------------------开始进行查询逻辑处理");
        Page<Map<String, Object>> page = new Page<Map<String, Object>>(current, number);

        logger.info("----------------------------------查询结果打印" + page.setRecords(this.groupMapper.dyGetGroupListByName(page, 1, group_name)));
        return page.setRecords(this.groupMapper.dyGetGroupListByName(page, 1, group_name));

    }

    /**
     * 根据id删除活动
     *
     * @param id 客户id
     * @return 0-->成功
     * @author Litq
     * @version 1.0
     */
    @Override
    public int deleteById(int id) {

        logger.info("----------------------------------开始进行删除逻辑处理");
        logger.info("----------------------------------打印要删除的id号码:" + id);
        int sign = groupMapper.deleteById(id);
        //删除活动选择的用户
        int sign1 = groupMapper.deleteCustomerByid(id);
        logger.info("----------------------------------返回结果:" + sign);
        logger.info("----------------------------------返回结果:" + sign1);
        if(sign == 1 && sign1 == 1){
            return sign;
        }
        else
        {
            return 0;
        }

    }

    /**
     * 根据id查询活动
     *
     * @param id 客户id
     * @return Map
     * @author Litq
     * @version 1.0
     */

    @Override
    public Map queryAllGroupById(String id) {
        logger.info("----------------------------------开始进行查询逻辑处理");
        GroupTable gt = groupMapper.selectById(id);
        Map<String, Object> map = new HashMap();
        if (gt != null) {
            BeanMap beanMap = BeanMap.create(gt);
            for (Object key : beanMap.keySet()) {
                map.put(key + "", beanMap.get(key));
            }
        }
        logger.info("----------------------------------打印查询结果" + map);
        return map;
    }
    /**
     * 根据id修改活动
     *
     * @param map 客户信息
     * @return Map
     * @author Litq
     * @version 1.0
     */

    @Override
    public int updateGroupById(Map map) {
        logger.info("----------------------------------开始进行插入逻辑处理");
        GroupTable gt = new GroupTable();
        gt.setGroupName(map.get("groupName").toString());
        gt.setGroupAddress(map.get("groupAddress").toString());
        gt.setZongCount(map.get("groupCount").toString());
        gt.setPintuanbegDate(map.get("pintuanbeg_date").toString());
        gt.setPintuanendDate(map.get("pintuanend_date").toString());
        gt.setGroupbegDate(map.get("groupbegDate").toString());
        gt.setGroupInfo(map.get("groupInfo").toString());
        gt.setId(Integer.parseInt(map.get("id").toString()));
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        String creatDate = sdf.format(date);
        gt.setCreateDate(creatDate);

        int sign = groupMapper.updateById(gt);
        logger.info("----------------------------------插入结果"+sign);
        return sign;
    }

    /**
     * 根据id添加活动
     *
     * @param map 客户信息
     * @return Map
     * @author Litq
     * @version 1.0
     */
    @Override
    public int insertGroup(Map map) {
        logger.info("----------------------------------开始进行插入逻辑处理");
        GroupTable gt = new GroupTable();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        String creatDate = sdf.format(date);
        gt.setCreateDate(creatDate);
        gt.setGroupbegDate(map.get("groupbegDate").toString());
        gt.setPintuanendDate(map.get("pintuanend_date").toString());
        gt.setPintuanbegDate(map.get("pintuanbeg_date").toString());
        gt.setZongCount(map.get("groupCount").toString());
        gt.setGroupAddress(map.get("groupAddress").toString());
        gt.setGroupName(map.get("groupName").toString());
        gt.setGroupInfo(map.get("groupInfo").toString());
        gt.setPintuanCount("0");
        int sign = groupMapper.insert(gt);
        logger.info("----------------------------------打印标志"+sign);
        return sign;
    }

}
